<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\Request;
/**
 * 执法变更记录
 *
 * @icon fa fa-circle-o
 */
class Violationlog extends Backend
{

    /**
     * Violationlog模型对象
     * @var \app\admin\model\Violationlog
     */
    protected $model = null;
    protected $noNeedRight = ['index'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Violationlog;
        $this->view->assign("isIllegalList", $this->model->getIsIllegalList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $where_find='';
             //项目名称检索
             $filter = $this->request->get('filter');
             $op = $this->request->get("op", '', 'trim');
             if($filter){
                 $filter = json_decode($filter,true);
 
                 $op = json_decode($op,true);
                 if(isset($filter['product.title']) && $filter['product.title']){
                    //find_in_set查询
                    $product_id=db('product')->where('title',$filter['product.title'])->value('id');
                    if(!$product_id)
                    {
                        $product_id='0';
                    }
                    $where_find='find_in_set('.$product_id.',product_ids)';
                    unset($filter['product.title']);
                    unset($op['product.title']);
                 }
                 $filter = json_encode($filter);
                 $op = json_encode($op);
                 Request::instance()->get(['filter'=>$filter]);
                 Request::instance()->get(['op'=>$op]);
             }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $ids = $this->request->get('ids','');
            $where_new=[];
            if($ids)
            {
                $where_new['violation_id']=$ids;
            }

            $list = $this->model
                    ->with(['store','car','product','zhifa'])
                    ->where($where)
                    ->where($where_new)
                    ->where($where_find)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('store')->visible(['store_name']);
				$row->getRelation('car')->visible(['plate_number']);
                //获取项目信息
                $product=db('product')->where('id','in',$row['product_ids'])->column('title');
                $products=implode(',',$product);
				$row->getRelation('product')->visible(['title']);
                $row->data('product.title',$products);
				$row->getRelation('zhifa')->visible(['name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
     /**
     * 编辑
     *
     * @param $ids
     * @return string
     * @throws DbException
     * @throws \think\Exception
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        if (false === $this->request->isPost()) {
           //获取车辆信息
            $row['plate_number']=db('car')->where(['id'=>$row['car_id']])->value('plate_number');
            //获取运输公司信息
            $row['store_name']=db('store')->where(['id'=>$row['store_id']])->value('store_name');
            //获取项目信息
            $product=db('product')->where('id','in',$row['product_ids'])->column('title');
            $row['product']=implode(',',$product);
            //获取执法人员信息
            $row['zhifa_name']=db('zhifa')->where(['id'=>$row['store_id']])->value('name');
            $row['imagelist']=explode(',',$row['pic']);
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $params = $this->preExcludeFields($params);
        $result = false;
        Db::startTrans();
        try {
            //是否采用模型验证
            if ($this->modelValidate) {
                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                $row->validateFailException()->validate($validate);
            }
            
            $result = $row->allowField(true)->save($params);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }

}
